package com.lb.zookeeper.demo;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * @Title: CuratorWatcherDemo
 * @Description: TODO
 * @Author lb
 * @Date 2019/04/04  18:32
 */
public class CuratorWatcherDemo {
    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder().connectString("92.168.245.130:2181,192.168.245.131:2181,192.168.245.132:2181")
                .sessionTimeoutMs(4000).retryPolicy(new
                        ExponentialBackoffRetry(1000, 3)).namespace("curator").build();
        curatorFramework.start();

        //当前节点的创建和修改事件监听，监听创建和修改
        addListenerWithNodeCache(curatorFramework, "/lb");
        //子节点的增加-CHILD_ADDED、修改-CHILD_UPDATED、删除-CHILD_REMOVED 的事件监听
        addListenerWithPathChildCache(curatorFramework,"/lb1");

        //综合节点监听事件
        addListenerWithTreeCache(curatorFramework, "/lb2");
        System.in.read();
    }

    /**
     * PathChildCache 监听一个节点下子节点的创建、删除、更新
     * NodeCache 监听一个节点的更新和创建事件
     * TreeCache  综合PathChildCache和NodeCache的特性
     */

    public static void addListenerWithNodeCache(CuratorFramework curatorFramework, String path) {
        final NodeCache nodeCache = new NodeCache(curatorFramework, path, false);
        NodeCacheListener nodeCacheListener = new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("监听事件" + nodeCache.getCurrentData().getPath());
            }
        };
        nodeCache.getListenable().addListener(nodeCacheListener);
        try {
            nodeCache.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void addListenerWithTreeCache(CuratorFramework curatorFramework, String path) throws Exception {
        TreeCache treeCache = new TreeCache(curatorFramework, path);
        TreeCacheListener treeCacheListener = new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
                System.out.println(event.getType() + "->" + event.getData().getPath());
            }
        };

        treeCache.getListenable().addListener(treeCacheListener);
        treeCache.start();
    }

    public static void addListenerWithPathChildCache(CuratorFramework curatorFramework, String path) throws Exception {
        PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework, path, true);

        PathChildrenCacheListener pathChildrenCacheListener = new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                System.out.println("Receive Event:" + event.getType());
            }
        };

        pathChildrenCache.getListenable().addListener(pathChildrenCacheListener);
        pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);

    }

}
